package com.erytech.linktalk.web.client.datasource;

import com.erytech.linktalk.web.client.framework.CompoundDataSource;
import com.erytech.linktalk.web.client.service.UserInfo;

public class RoomDataSource extends CompoundDataSource {
	
	static final String USER_DS = "userDS";

	static final String MESSAGE_DS = "messageDS";
	
	static final String PORTRAIT_DS = "portraitDS";

  static final String SMILEY_DS = "smileyDS";
	
	private String roomId;
	
	public RoomDataSource(String roomId) {
		this.roomId = roomId;
		putDataSource(USER_DS, new UserDataSource(this));
		putDataSource(MESSAGE_DS, new MessageDataSource(this));
		putDataSource(PORTRAIT_DS, new PortraitDataSource());
		putDataSource(SMILEY_DS, new SmileyDataSource());
	}

	public String getRoomId() {
		return roomId;
	}

	public UserDataSource getUserDataSource() {
		return (UserDataSource)getDataSource(USER_DS);
	}

	public MessageDataSource getMessageDataSource() {
		return (MessageDataSource)getDataSource(MESSAGE_DS);
	}
	
	public PortraitDataSource getPortraitDataSource() {
		return (PortraitDataSource)getDataSource(PORTRAIT_DS);
	}

	public void enterRoom(final UserInfo userInfo) {
		fireDataUpdateEvent(new RoomUpdateEvent(this, RoomUpdateEvent.ENTER_ROOM, userInfo));
		getUserDataSource().startPolling();
   getMessageDataSource().startPolling();
	}

	public void leftRoom() {
		getUserDataSource().stopPolling();
		getMessageDataSource().stopChating();
		fireDataUpdateEvent(new RoomUpdateEvent(this, RoomUpdateEvent.LEFT_ROOM));
	}

	public SmileyDataSource getSmileyDataSource() {
		return (SmileyDataSource)getDataSource(SMILEY_DS);
	}
	
}